﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AvatarHelper;
using Avatar.Model;

namespace Avatar.ResumeService
{
    public static class LanguageLevelManager
    {
        public static void SaveLanguageLevel(
            string _user,
            int _english,
            int _japanese,
            string _toefl,
            string _gre,
            string _gmart,
            string _ielts
            )
        {
            #region 入口检测

            if (!string.IsNullOrEmpty(_toefl) && _toefl.Length > 10) throw new Exception("TOEFL内容不能超过10个字符");
            if (!string.IsNullOrEmpty(_gre) && _gre.Length > 10) throw new Exception("GRE内容不能超过10个字符");
            if (!string.IsNullOrEmpty(_gmart) && _gmart.Length > 10) throw new Exception("GMART内容不能超过10个字符");
            if (!string.IsNullOrEmpty(_ielts) && _ielts.Length > 10) throw new Exception("IELTS内容不能超过10个字符");

            #endregion
            try
            {
                using (var _adc = DBHelper.GetDataContext())
                {
                    var _resume = _adc.Resume.Where(r => r.User == _user).FirstOrDefault();
                    if (_resume != null)
                    {
                        if (_resume.LanguageLevel == null)
                        {
                            _resume.LanguageLevel = new LanguageLevel();

                            _resume.LanguageLevel.English = _english;
                            _resume.LanguageLevel.Japanese = _japanese;
                            _resume.LanguageLevel.GMAT = _gmart;
                            _resume.LanguageLevel.GRE = _gre;
                            _resume.LanguageLevel.IELTS = _ielts;
                            _resume.LanguageLevel.TOEFL = _toefl;
                            _resume.LanguageLevel.ID = Guid.NewGuid();

                            _adc.LanguageLevel.InsertOnSubmit(_resume.LanguageLevel);
                        }
                        else
                        {
                            _resume.LanguageLevel.English = _english;
                            _resume.LanguageLevel.Japanese = _japanese;
                            _resume.LanguageLevel.GMAT = _gmart;
                            _resume.LanguageLevel.GRE = _gre;
                            _resume.LanguageLevel.IELTS = _ielts;
                            _resume.LanguageLevel.TOEFL = _toefl;
                        }
                        _adc.SubmitChanges();
                    }
                }
            }
            catch (Exception _e)
            {
                throw new Exception("编辑语言能力出错："+_e.Message);
            }
        }
        /// <summary>
        /// 获取语言能力等级
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public static LanguageLevel GetLanguageLevel(Guid _id)
        {
            return DBHelper.Get<LanguageLevel>(l => l.ID == _id, "获取语言能力等级失败：");
        }
        /// <summary>
        /// 删除语言能力等级
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public static void Delete(Guid _id)
        {
            try
            {
                using (var _adc = DBHelper.GetDataContext())
                {
                    var _l = _adc.LanguageLevel.Where(l => l.ID == _id).FirstOrDefault();

                    _adc.LanguageLevel.DeleteOnSubmit(_l);

                    _adc.SubmitChanges();
                }
            }
            catch (Exception _e)
            {
                throw new Exception("删除语言能力等级出错:" + _e.Message);
            }
        }
    }
}
